package com.hllxd.hism.star.basic.service.service;

import com.alibaba.fastjson.JSON;
import com.hllxd.hism.star.basic.service.dao.mapper.mis.UserGroupMapper;
import com.hllxd.hism.star.basic.service.model.UserGroupInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author xiyong.lxy
 * @date 2020-10-12 16:51
 * v1/user/userInfo.do
 */
@Slf4j
@Service("userGroupService")
public class UserGroupServiceImpl implements UserGroupService {

    @Autowired
    private UserGroupMapper userGroupMapper;

    @Autowired
    private RoleService roleService;

    @Override
    public List<UserGroupInfo> userGroupList() {
        return userGroupMapper.userGroupList();
    }

    @Override
    public Long setUserGroup(UserGroupInfo entity) {
        log.info("set param: {}", JSON.toJSONString(entity));
        if (entity.id == null || entity.id == 0) {
            return userGroupMapper.insertUserGroup(entity);
        } else {
//            UserGroupInfo originUserGroup = userGroupMapper.getOneUserGroupInfo(entity.getId());
//            roleService.updateRoleUserGroupIdCache(entity.getId(), originUserGroup.getUserNames(), entity.getUserNames());
            return userGroupMapper.updateUserGroup(entity);
        }
    }

    @Override
    public Long delUserGroup(Long id) {
//        UserGroupInfo originUserGroup = userGroupMapper.getOneUserGroupInfo(id);
//        roleService.updateRoleUserGroupIdCache(id, originUserGroup.getUserNames(), "");
        roleService.delRoleRoleUserGroupIdMysql(id);
        return userGroupMapper.updateUserGroupAction(id);
    }

    @Override
    public UserGroupInfo getUserGroupInfo(Long id) {
        return userGroupMapper.getOneUserGroupInfo(id);
    }
}
